//
//  SDPaientItemCell.m
//  SKinDiary
//
//  Created by pcyang on 16/7/7.
//  Copyright © 2016年 tencent. All rights reserved.
//

#import "SDPaientItemCell.h"
#import "SDSimpleImageView.h"
#import "SDPaientInfoItem.h"

#define kDefaultAvatarName      @"img_profile_avator_defult.png"

#define kLeftMarginWidth             (13.f)
#define kRightSpace             (10.f)
#define kImgToNameSpace         (20.f)

#define kAvatarWidth            (50.f)
#define kAvatarHeight           (50.f)

@interface SDPaientItemCell ()

@property (nonatomic, strong) SDAvatarView *avatarView;
@property(nonatomic,strong) UILabel  *nameLabel;
@property(nonatomic,strong) UILabel  *idLabel;

@end


@implementation SDPaientItemCell
-(id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self)
    {
        _avatarView = [[SDAvatarView alloc] initWithFrame:CGRectMake(kLeftMarginWidth, 0, kAvatarWidth, kAvatarHeight)];
        _avatarView.top = (kPaientCellHeight- kAvatarHeight)/2;
        _avatarView.defaultImage = [UIImage imageNamed:@"avatar_empty.png"];
        [self.contentView addSubview:_avatarView];
        
        _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(_avatarView.right + kLeftMarginWidth, _avatarView.top + 4, kScreenWidth- _avatarView.right - 2*kLeftMarginWidth, 15)];
        [_nameLabel setFont:[UIFont systemFontOfSize:15.f]];
        [_nameLabel setTextColor:[UIColor colorWithRGB:0x747f8b]];
        [self.contentView addSubview:_nameLabel];
        
        _idLabel = [[UILabel alloc] initWithFrame:CGRectMake(_nameLabel.left, _nameLabel.bottom + 8, kScreenWidth - _avatarView.right - 2*kLeftMarginWidth, 13)];
        [_idLabel setFont:[UIFont systemFontOfSize:13.f]];
        [_idLabel setTextColor:[UIColor colorWithRGB:0xbcbec8]];
        [self.contentView addSubview:_idLabel];
        
        UIView* lineBottom = [[UIView alloc] initWithFrame:CGRectMake(_nameLabel.left, kPaientCellHeight - 1.f, kScreenWidth - 20.f, 1.f)];
        lineBottom.backgroundColor = RGB(0xf3, 0xf4, 0xf4);
        [self.contentView addSubview:lineBottom];
    }
    
    return self;
}

-(void)setCellData:(SDPaientInfoItem*)paientInfoItem
{
    if (CHECK_VALID_STRING(paientInfoItem.avatar)) {
        NSString* directory = [SDFile getDirectoryInCacheWithName:kAvatarDirectoryName];
        NSString *filePath = [directory stringByAppendingPathComponent:paientInfoItem.avatar];
        if ([SDFile fileExist:filePath]) {
            _avatarView.image = [_avatarView modifiedImageWithImage:[UIImage imageWithContentsOfFile:filePath]];
        }
        else
        {
            [_avatarView setImageWithURL:nil];
        }
    } else {
        [_avatarView setImageWithURL:nil];
    }

//    [_avatarView setImageWithURL:paientInfoItem.avatar];

    [_nameLabel setText:paientInfoItem.name];
    [_nameLabel sizeToFit];
    
    _idLabel.text = [NSString stringWithFormat:@"ID:%@",paientInfoItem.cId];
    [_idLabel sizeToFit];
}
@end
